package ecnu.ecgbackend.config.security;

import ecnu.ecgbackend.config.security.filter.CheckTokenFilter;
import ecnu.ecgbackend.config.security.filter.CustomAuthenticationFilter;
import ecnu.ecgbackend.config.security.handler.AnonymousAuthenticationHandler;
import ecnu.ecgbackend.config.security.handler.CustomerAccessDeniedHandler;
import ecnu.ecgbackend.config.security.handler.LoginFailureHandler;
import ecnu.ecgbackend.config.security.handler.LoginSuccessHandler;
import ecnu.ecgbackend.config.security.service.CustomerUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;


import javax.annotation.Resource;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    @Resource
    private AnonymousAuthenticationHandler anonymousAuthenticationHandler;
    @Resource
    CustomerAccessDeniedHandler customerAccessDeniedHandler;
    @Resource
    private CheckTokenFilter checkTokenFilter;

    @Bean
    public BCryptPasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
    @Override
    protected  void configure(HttpSecurity http) throws Exception{
        http.csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers("/account/login").permitAll()
                .antMatchers("/account/register").permitAll()
                .antMatchers("/common/code").permitAll()
                .antMatchers("/common/verify").permitAll()
                .antMatchers("/common/verify/code").permitAll()
                .antMatchers("/account/password").permitAll()
                .anyRequest().authenticated();

        http.addFilterBefore(checkTokenFilter,UsernamePasswordAuthenticationFilter.class);
        http.exceptionHandling()
                .authenticationEntryPoint(anonymousAuthenticationHandler)
                .accessDeniedHandler(customerAccessDeniedHandler);
        http.cors();
    }

   @Bean
   @Override
   public AuthenticationManager authenticationManagerBean() throws Exception{
        return super.authenticationManagerBean();
   }
}
